package com.example.musicrecommend.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.musicrecommend.common.ErrorCode;
import com.example.musicrecommend.constant.CommonConstant;
import com.example.musicrecommend.exception.BusinessException;
import com.example.musicrecommend.exception.ThrowUtils;
import com.example.musicrecommend.mapper.SongMapper;
import com.example.musicrecommend.model.dto.song.SongQueryRequest;
import com.example.musicrecommend.model.dto.song.SongSearchRequest;
import com.example.musicrecommend.model.entity.Song;
import com.example.musicrecommend.model.vo.SongVO;
import com.example.musicrecommend.service.SongService;
import com.example.musicrecommend.utils.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.io.Serializable;

/**
* @author PERSON
* @description 针对表【song(歌曲)】的数据库操作Service实现
* @createDate 2024-10-02 00:41:20
*/
@Service
public class SongServiceImpl extends ServiceImpl<SongMapper, Song>
    implements SongService{


    @Override
    public QueryWrapper<SongVO> getQueryWrapper(SongQueryRequest songQueryRequest) {
        if (songQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        Long id = songQueryRequest.getId();
        String name = songQueryRequest.getName();
        String singerName = songQueryRequest.getSingerName();
        String tags = songQueryRequest.getTags();

        String sortField = songQueryRequest.getSortField();
        String sortOrder = songQueryRequest.getSortOrder();

        QueryWrapper<SongVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(id != null && id != 0, "id", id);
        queryWrapper.like(StringUtils.isNotBlank(name), "s.name", name);
        queryWrapper.like(StringUtils.isNotBlank(singerName), "si.name", singerName);
        queryWrapper.like(StringUtils.isNotBlank(tags), "tags", tags);

        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);
        return queryWrapper;
    }

    @Override
    public QueryWrapper<SongVO> getQueryWrapper(SongSearchRequest songSearchRequest) {
        if (songSearchRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }

        String text = songSearchRequest.getText();

        String sortField = songSearchRequest.getSortField();
        String sortOrder = songSearchRequest.getSortOrder();

        QueryWrapper<SongVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(wrapper -> wrapper.like(StringUtils.isNotBlank(text), "s.name", text)
                .or().like(StringUtils.isNotBlank(text), "si.name", text)
                .or().like(StringUtils.isNotBlank(text), "tags", text));
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);
        return queryWrapper;
    }

    @Override
    public void validSong(Song song, boolean isAdd) {
        ThrowUtils.throwIf(song == null, ErrorCode.PARAMS_ERROR);
        Long id = song.getId();
         String name = song.getName();
         Long singerId = song.getSingerId();
         //Long tagId
         String tags = song.getTags();
         String url = song.getUrl();

        if (isAdd) {
            ThrowUtils.throwIf(StringUtils.isAnyBlank(name,url), ErrorCode.PARAMS_ERROR);
        } else {
            ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        }

        if (StringUtils.isNotBlank(name) && name.length() > 256) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "歌曲名字过长");
        }
        if (StringUtils.isNotBlank(tags) && tags.length() > 256) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "歌曲标签过长");
        }
        if (StringUtils.isNotBlank(url) && url.length() > 256) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "歌曲url过长");
        }

    }

    @Override
    public SongVO getSongById(Serializable id) {
        return this.getBaseMapper().getOneSongById(id);
    }

    @Override
    public Page<SongVO> selectSongsPage(Page<SongVO> page, Wrapper<SongVO> queryWrapper) {
        return this.getBaseMapper().selectSongsPage(page,queryWrapper);
    }


}




